import axios from "axios";
//axios实例
const instance=axios.create({
    baseURL:'http://localhost:8080',
    timeout:5000,
})
//请求拦截器
instance.interceptors.request.use(
    config =>{
        const token=localStorage.getItem('token')
        if(token)  config.headers.Authorization=token
        return config //会包装成Promise对象
    },
    error =>{
        return Promise.reject(err) //暴力解决
    }
)
//响应拦截器
instance.interceptors.response.use(
    res =>{
        if(res.status===200){
            return res.data
        }
        else 
            return Promise.reject(res.data)
    },
    err=>{
        return Promise.reject(err)
    }
)
//封装get和post请求
const get=async(url,params={})=>{
    return instance.get(url,params)
}
export {get}


